/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_analyze.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/17 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/oracle/config.h"
#include "../test_ora_comm.h"
#include <oracle/ora_core.h>
#include <oracle/comm/ora_comm.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <frservice_mng.h>
#include <frcomm.h>

extern ApmContext g_st_apm_context;
extern void *g_p_apm_context;

extern int ora_analyze(Conntrack *p_st_conn
                    , Apm_packet *p_st_packet
                    , Frhash_header *p_st_hash_report);

static int __suite_init(void)
{
    frservice_mng_init();
    frservice_setRef(g_st_apm_context.p_st_service_ref);
    g_p_apm_context = &g_st_apm_context;
    return 0;
}

static int __suite_cleanup(void)
{
    frservice_mng_destory();
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    Frvar_field *p_st_field = NULL;
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);

    // tns connect req
    char *p_bin = (char *) "00e7000001000000013b012c0c412000ffff4f980000000100a10046000000008181000000000000000000000000000000000000000000000000000020000020000000000000284445534352495054494f4e3d28414444524553533d2850524f544f434f4c3d5443502928484f53543d3137322e31362e302e37372928504f52543d31353231292928434f4e4e4543545f444154413d284349443d2850524f4752414d3d4a444243205468696e20436c69656e742928484f53543d5f5f6a6462635f5f2928555345523d57696e292928534552564943455f4e414d453d7064626f72636c292929";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 100000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // tns accept resp
    p_bin = (char *) "0029000002000000013b084100000000010000000029e1010000000000000000000020000020000000";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 101000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // pro req
    p_bin = (char *) "0000002106200000000001060504030201004a6176615f5454432d382e322e3000";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 102000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // pro resp
    p_bin = (char *) "000000f006000000000001060049424d50432f57494e5f4e5436342d392e312e30006903010a006603400301400366030166034803014803660301660352030152036603016603610301610366030166031f03081f0366030100640000006001240f050b0c030c0c0504050d0609070805050505050f05050505050a050505050504050607080823472347081123081141b0470083036907d0030000000000000000000000000000000000000000000000000000000000000000000000000028060101011f010107010101010101017fff030b03070101ff01ffff010a0101ff01050920017f04000702010001180003";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 103000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // dty req
    p_bin = (char *) "00000a2806200000000002690369030126060100001a0101070101010101010029900307030001004f013704010000001c00000920013b070201000000000180000000443c800000000000000000000100010001000000020002000a00000008000800010000000c000c000a0000001700170001000000180018000100000019001900010000001a001a00010000001b001b00010000001c001c00010000001d001d00010000001e001e00010000001f001f0001000000200020000100000021002100010000000a000a00010000000b000b00010000002800280001000000290029000100000075007500010000007800780001000001220122000100000123012300010000012401240001000001250125000100000126012600010000012a012a00010000012b012b00010000012c012c00010000012d012d00010000012e012e00010000012f012f000100000130013000010000013101310001000001320132000100000133013300010000013401340001000001350135000100000136013600010000013701370001000001380138000100000139013900010000013b013b00010000013c013c00010000013d013d00010000013e013e00010000013f013f000100000140014000010000014101410001000001420142000100000143014300010000014701470001000001480148000100000149014900010000014b014b00010000014d014d00010000014e014e00010000014f014f000100000150015000010000015101510001000001520152000100000153015300010000015401540001000001550155000100000156015600010000015701570001000001580158000100000159015900010000015a015a00010000015c015c00010000015d015d00010000016201620001000001630163000100000167016700010000016b016b00010000017c017c00010000017d017d00010000017e017e00010000017f017f00010000018001800001000001810181000100000182018200010000018301830001000001840184000100000185018500010000018601860001000001870187000100000189018900010000018a018a00010000018b018b00010000018c018c00010000018d018d00010000018e018e00010000018f018f00010000019001900001000001910191000100000194019400010000019501950001000001960196000100000197019700010000019d019d00010000019e019e00010000019f019f0001000001a001a00001000001a101a10001000001a201a20001000001a301a30001000001a401a40001000001a501a50001000001a601a60001000001a701a70001000001a801a80001000001a901a90001000001aa01aa0001000001ab01ab0001000001ad01ad0001000001ae01ae0001000001af01af0001000001b001b00001000001b101b10001000001c101c10001000001c201c20001000001c601c60001000001c701c70001000001c801c80001000001c901c90001000001ca01ca0001000001cb01cb0001000001cc01cc0001000001cd01cd0001000001ce01ce0001000001cf01cf0001000001d201d20001000001d301d30001000001d401d40001000001d501d50001000001d601d60001000001d701d70001000001d801d80001000001d901d90001000001da01da0001000001db01db0001000001dc01dc0001000001dd01dd0001000001de01de0001000001df01df0001000001e001e00001000001e101e10001000001e201e20001000001e301e30001000001e401e40001000001e501e50001000001e601e60001000001ea01ea0001000001eb01eb0001000001ec01ec0001000001ed01ed0001000001ee01ee0001000001ef01ef0001000001f001f00001000001f201f20001000001f301f30001000001f401f40001000001f501f50001000001f601f60001000001fd01fd0001000001fe01fe0001000002010201000100000202020200010000020402040001000002050205000100000206020600010000020702070001000002080208000100000209020900010000020a020a00010000020b020b00010000020c020c00010000020d020d00010000020e020e00010000020f020f000100000210021000010000021102110001000002120212000100000213021300010000021402140001000002150215000100000216021600010000021702170001000002180218000100000219021900010000021a021a00010000021b021b00010000021c021c00010000021d021d00010000021e021e00010000021f021f0001000002300230000100000235023500010000023c023c00010000023d023d00010000023e023e00010000023f023f000100000240024000010000024202420001000002330233000100000234023400010000024302430001000002440244000100000245024500010000024602460001000002470247000100000248024800010000024902490001000000030002000a000000040002000a0000000500010001000000060002000a000000070002000a00000009000100010000000d0000000e0000000f001700010000001000000011000000120000001300000014000000150000001600000027007800010000003a000000440002000a00000045000000460000004a0000004c0000005b0002000a0000005e000100010000005f001700010000006000600001000000610060000100000064006400010000006500650001000000660066000100000068000b0001000000690000006a006a00010000006c006d00010000006d006d00010000006e006f00010000006f006f0001000000700070000100000071007100010000007200720001000000730073000100000074006600010000007600000077000000790000007a0000007b00000088000000920092000100000093000000980002000a000000990002000a0000009a0002000a0000009b000100010000009c000c000a000000ac0002000a000000b200b20001000000b300b30001000000b400b40001000000b500b50001000000b600b60001000000b700b70001000000b8000c000a000000b900b90001000000ba00ba0001000000bb00bb0001000000bc00bc0001000000bd00bd0001000000be00be0001000000bf000000c0000000c300700001000000c400710001000000c500720001000000d000d00001000000d1000000e700e70001000000e800e70001000000e900e90001000000f1006d0001000002030000024e024e00010000024f024f00010000025002500001000002650265000100000266026600010000026702670001000002680268000100000263026300010000025102510001000002520252000100000253025300010000025402540001000002550255000100000256025600010000025702570001000002580258000100000259025900010000025a025a00010000025b025b00010000025c025c00010000025d025d00010000026e026e00010000026f026f000100000270027000010000027102710001000002720272000100000273027300010000027402740001000002750275000100000276027600010000027702770001000002780278000100000000";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 104000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // dty resp
    p_bin = (char *) "00000a300600000000000280000000373c3c8000000000000012000100010001000000020002000a00000008000800010000000c000c000a0000001700170001000000180018000100000019001900010000001a001a00010000001b001b00010000001c001c00010000001d001d00010000001e001e00010000001f001f0001000000200020000100000021002100010000000a000a00010000000b000b00010000002800280001000000290029000100000075007500010000007800780001000001220122000100000123012300010000012401240001000001250125000100000126012600010000012a012a00010000012b012b00010000012c012c00010000012d012d00010000012e012e00010000012f012f000100000130013000010000013101310001000001320132000100000133013300010000013401340001000001350135000100000136013600010000013701370001000001380138000100000139013900010000013b013b00010000013c013c00010000013d013d00010000013e013e00010000013f013f000100000140014000010000014101410001000001420142000100000143014300010000014701470001000001480148000100000149014900010000014b014b00010000014d014d00010000014e014e00010000014f014f000100000150015000010000015101510001000001520152000100000153015300010000015401540001000001550155000100000156015600010000015701570001000001580158000100000159015900010000015a015a00010000015c015c00010000015d015d00010000016201620001000001630163000100000167016700010000016b016b00010000017c017c00010000017d017d00010000017e017e00010000017f017f00010000018001800001000001810181000100000182018200010000018301830001000001840184000100000185018500010000018601860001000001870187000100000189018900010000018a018a00010000018b018b00010000018c018c00010000018d018d00010000018e018e00010000018f018f00010000019001900001000001910191000100000194019400010000019501950001000001960196000100000197019700010000019d019d00010000019e019e00010000019f019f0001000001a001a00001000001a101a10001000001a201a20001000001a301a30001000001a401a40001000001a501a50001000001a601a60001000001a701a70001000001a801a80001000001a901a90001000001aa01aa0001000001ab01ab0001000001ad01ad0001000001ae01ae0001000001af01af0001000001b001b00001000001b101b10001000001c101c10001000001c201c20001000001c601c60001000001c701c70001000001c801c80001000001c901c90001000001ca01ca0001000001cb01cb0001000001cc01cc0001000001cd01cd0001000001ce01ce0001000001cf01cf0001000001d201d20001000001d301d30001000001d401d40001000001d501d50001000001d601d60001000001d701d70001000001d801d80001000001d901d90001000001da01da0001000001db01db0001000001dc01dc0001000001dd01dd0001000001de01de0001000001df01df0001000001e001e00001000001e101e10001000001e201e20001000001e301e30001000001e401e40001000001e501e50001000001e601e60001000001ea01ea0001000001eb01eb0001000001ec01ec0001000001ed01ed0001000001ee01ee0001000001ef01ef0001000001f001f00001000001f201f20001000001f301f30001000001f401f40001000001f501f50001000001f601f60001000001fd01fd0001000001fe01fe0001000002010201000100000202020200010000020402040001000002050205000100000206020600010000020702070001000002080208000100000209020900010000020a020a00010000020b020b00010000020c020c00010000020d020d00010000020e020e00010000020f020f000100000210021000010000021102110001000002120212000100000213021300010000021402140001000002150215000100000216021600010000021702170001000002180218000100000219021900010000021a021a00010000021b021b00010000021f021f0001000002200000022100000222000002230000022400000225000002260000022700000228000002290000022a0000022b0000022c0000022d0000022e0000022f0000023002300001000002310000023200000233023300010000023402340001000002360000023700000238000002390000023a0000023b0000023c023c00010000023d023d00010000023e023e00010000023f023f0001000002400240000100000241000002420242000100000243024300010000024402440001000002450245000100000246024600010000024702470001000002480248000100000249024900010000024a0000024b0000024c0000024d0000024f024f00010000025102510001000002520252000100000253025300010000025402540001000002550255000100000256025600010000025702570001000002580258000100000259025900010000025a025a00010000025b025b00010000025c025c00010000025d025d000100000263026300010000026400000265026500010000026602660001000002670267000100000268026800010000026e026e00010000026f026f0001000002700270000100000271027100010000027202720001000002730273000100000274027400010000027502750001000002760276000100000277027700010000027802780001000000030002000a000000040002000a0000000500010001000000060002000a000000070002000a00000009000100010000000d0000000e0000000f001700010000001000000011000000120000001300000014000000150000001600000027007800010000003a000000440002000a00000045000000460000004a0000004c0000005b0002000a0000005e000100010000005f001700010000006000600001000000610060000100000064006400010000006500650001000000660066000100000068000000690000006a006a00010000006c006d00010000006d006d00010000006e006f00010000006f006f00010000007000700001000000710071000100000072007200010000007300730001000000740066000100000076000000790000007a0000007b00000088000000920092000100000093000000980002000a000000990002000a0000009a0002000a0000009b000100010000009c000c000a000000ac0002000a000000b200b20001000000b300b30001000000b400b40001000000b500b50001000000b600b60001000000b700b70001000000b8000c000a000000b9000000ba000000bb000000bc000000bd000000be000000bf000000c0000000c300700001000000c400710001000000c500720001000000d000d00001000000d1000000e700e70001000000e800e70001000000e900e90001000000f1006d0001000000f5000000f6000000fa000000fb000000fc0000020300000000";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 105000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // ttc8 logon req
    p_bin = (char *) "0000009d0620000000000376010101080101010105010150444241444d494e010d0d415554485f5445524d494e414c010707756e6b6e6f776e00010f0f415554485f50524f4752414d5f4e4d0110104a444243205468696e20436c69656e7400010c0c415554485f4d414348494e450105054e6f6e6b7200010808415554485f5049440104043132333400010808415554485f53494401030357696e00";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 106000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // ttc8 logon resp
    p_bin = (char *) "0000010a060000000000080103010c0c415554485f534553534b455901606033444232394334324638303442413032383939444130383242464644343338423333333430323736363830323731433131444434413541303831313741353038313537303641434242343436304142383545363132423334313039344242373200010d0d415554485f5646525f444154410114143330383630313634444630373934423441443243021b25011a1a415554485f474c4f42414c4c595f554e495155455f444249440001202043353433353534373635393331384542343831443636413430303633373846390004010101020000000000000000000000000000000000000001000000000000";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 107000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // ttc8 logon req
    p_bin = (char *) "000003da06200000000003730201010802010101010d010150444241444d494e010d0d415554485f50415353574f52440140404442434135323332364242333332453234383138383638343030334538463743394141433731333739453635363937303034323931344530324235314137313400010d0d415554485f5445524d494e414c010707756e6b6e6f776e00010c0c415554485f534553534b45590160604532323232343342323931364231314543393138383132463931353535433639433735303843433237433432424134304336373230313334314635433145414133363235373538444234313632363042433332394545323345393845423344320101010f0f415554485f50524f4752414d5f4e4d0110104a444243205468696e20436c69656e7400010c0c415554485f4d414348494e450105054e6f6e6b7200010808415554485f5049440104043132333400010808415554485f41434c0104043434303000011212415554485f414c5445525f53455353494f4e016464414c5445522053455353494f4e205345542054494d455f5a4f4e453d27417369612f5368616e6768616927204e4c535f4c414e47554147453d2753494d504c4946494544204348494e45534527204e4c535f5445525249544f52593d274348494e412700010101161653455353494f4e5f434c49454e545f4c4f42415454520101013100011a1a53455353494f4e5f434c49454e545f4452495645525f4e414d450108086a6462637468696e0001161653455353494f4e5f434c49454e545f56455253494f4e01090932303233373534323400011313415554485f434f4e4e4543545f535452494e47016868284445534352495054494f4e3d28434f4e4e4543545f444154413d28534552564943455f4e414d453d5044424f52434c292928414444524553533d2850524f544f434f4c3d5443502928484f53543d3137322e31362e302e37372928504f52543d3135323129292900010e0e415554485f434f5059524947485401f1f1224f7261636c650a4576657279626f647920666f6c6c6f77730a53706565647920626974732065786368616e67650a537461727320617761697420746f20676c6f77220a54686520707265636564696e67206b657920697320636f707972696768746564206279204f7261636c6520436f72706f726174696f6e2e0a4475706c69636174696f6e206f662074686973206b6579206973206e6f7420616c6c6f77656420776974686f7574207065726d697373696f6e0a66726f6d204f7261636c6520436f72706f726174696f6e2e20436f707972696768742032303033204f7261636c6520436f72706f726174696f6e2e00";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 108000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // ttc8 logon resp
    p_bin = (char *) "000007f806000000000008012c011313415554485f56455253494f4e5f535452494e470112122d2036346269742050726f64756374696f6e00011010415554485f56455253494f4e5f53514c010202323200011313415554485f58414354494f4e5f5452414954530101013300010f0f415554485f56455253494f4e5f4e4f01090932303233373534323400011313415554485f56455253494f4e5f5354415455530101013000011515415554485f4341504142494c4954595f5441424c450000010f0f415554485f4c4153545f4c4f47494e011a1a373837323039304130333245323830303030303030303030303000010b0b415554485f44424e414d450107075044424f52434c00011111415554485f44425f4d4f554e545f494400010a0a3133383532363139393100010b0b415554485f44425f494400010a0a3233303436333030313900010c0c415554485f555345525f494401030331303200010f0f415554485f53455353494f4e5f4944010202333700010f0f415554485f53455249414c5f4e554d010505313232313900011010415554485f494e5354414e43455f4e4f0101013100011010415554485f4641494c4f5645525f49440101013100010f0f415554485f5345525645525f5049440104043330303000011313415554485f53435f5345525645525f484f5354010b0b77696e375f6f726131326300011515415554485f53435f4442554e495155455f4e414d450104046f72636c00011515415554485f53435f494e5354414e43455f4e414d450104046f72636c00011313415554485f53435f494e5354414e43455f49440101013100011b1b415554485f53435f494e5354414e43455f53544152545f54494d45012424323031342d30392d30352031343a33393a33332e303030303030303030202b30383a303000011111415554485f53435f44425f444f4d41494e0000011414415554485f53435f534552564943455f4e414d450107077064626f72636c00011b1b415554485f4f4e535f524c425f5355425343525f5041545445524e013434226576656e74547970653d64617461626173652f6576656e742f736572766963656d6574726963732f7064626f72636c2200000000011a1a415554485f4f4e535f48415f5355425343525f5041545445524e01494928226576656e74547970653d64617461626173652f6576656e742f736572766963652229207c2028226576656e74547970653d64617461626173652f6576656e742f686f737422290000011111415554485f53435f5356435f464c41475301030331333600011111415554485f494e5354414e43454e414d450104046f72636c00010f0f415554485f4e4c535f4c584c414e0001121253494d504c4946494544204348494e45534500011616415554485f4e4c535f4c58435445525249544f5259000105054348494e4100011515415554485f4e4c535f4c584343555252454e435900010303efbfa500011414415554485f4e4c535f4c584349534f43555252000105054348494e4100011515415554485f4e4c535f4c58434e554d4552494353000102022e2c00011313415554485f4e4c535f4c584344415445464d0001090944442d4d4f4e2d525200011515415554485f4e4c535f4c5843444154454c414e470001121253494d504c4946494544204348494e45534500011111415554485f4e4c535f4c5843534f52540001060642494e41525900011515415554485f4e4c535f4c584343414c454e44415200010909475245474f5249414e00011515415554485f4e4c535f4c5843554e494f4e43555200010303efbfa500011313415554485f4e4c535f4c584354494d45464d00010e0e48482e4d492e535358464620414d00011313415554485f4e4c535f4c584353544d50464d0001181844442d4d4f4e2d52522048482e4d492e535358464620414d00011313415554485f4e4c535f4c584354545a4e464d0001121248482e4d492e535358464620414d20545a5200011313415554485f4e4c535f4c584353545a4e464d00011c1c44442d4d4f4e2d52522048482e4d492e535358464620414d20545a5200011111415554485f5356525f524553504f4e534501606044303143363638364632334132433935423334433337443734444339304535344633354632313446313744423834313338453234394442433245463546343846333942323942304330434332384436353342343233413143334146453030463700011515415554485f4d41585f4f50454e5f435552534f525301030333303000010d0d415554485f5044425f55494400010a0a323330343633303031390017050101100113160001121253494d504c4946494544204348494e4553450110000105054348494e41010900010303efbfa500000105054348494e410101000102022e2c010200010808414c333255544638010a00010909475245474f5249414e010c0001090944442d4d4f4e2d525201070001121253494d504c4946494544204348494e45534501080001060642494e415259010b00010e0e48482e4d492e535358464620414d01390001181844442d4d4f4e2d52522048482e4d492e535358464620414d013a0001121248482e4d492e535358464620414d20545a52013b00011c1c44442d4d4f4e2d52522048482e4d492e535358464620414d20545a52013c00010303efbfa501340001060642494e41525901320001040442595445013d0001050546414c5345013e00010b0b800083e8bd3c3c8000000001a30004010101030000000000000000000000000000000000000002000000000000";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 109000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // Oversion req
    p_bin = (char *) "00000013062000000000033b03010201000101";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 110000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // Oversion resp
    p_bin = (char *) "000000b80600000000000801a14f7261636c652044617461626173652031326320456e74657270726973652045646974696f6e2052656c656173652031322e312e302e312e30202d2036346269742050726f64756374696f6e0a576974682074686520506172746974696f6e696e672c204f4c41502c20416476616e63656420416e616c797469637320616e64205265616c204170706c69636174696f6e2054657374696e67206f7074696f6e73040c1001000901010103";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 111000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // sql req
    p_bin = (char *) "000000d1062000000000035e010280290001013f01010d000004ffffffff010a047fffffff01010300000000000000000001000000000000000073656c656374206c6f632066726f6d206465707420776865726520646570746e6f3d3a312020616e6420646e616d653d3a322020616e64206c6f633d3a3320010100000000000001010002800000000002030000011600000000020369010001030000012800011000000203690100010300000120000110000002036901000702c10b0a4143434f554e54494e47084e455720594f524b";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 112000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    // sql resp
    p_bin = (char *) "000000c306000000000010174037638f31033b7b1103e2140588f12e7872090a0b2e28010d01015101800000010d0000000002036901010d01030103034c4f43000000000107077872090a0b2e3b0101021fe801020102000622010100010a00000007084e455720594f524b080106032dee8c0001020000000000000401010104010102057b000001020110030000000000000000000000000100010100000000214f52412d30313430333a20e69caae689bee588b0e4bbbbe4bd95e695b0e68dae0a";
    memset(arr_payload, 0x00, sizeof(arr_payload));
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    ui_payload_len = strlen(p_bin) / 2;
    p_st_packet = NULL;
    st_timestamp.tv_usec = 113000L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(ora_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    // 测试结果
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);

    p_st_node_report = FRLIST_LAST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_ISRESPREQUIRED, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, FR_YES);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_START_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354112LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_END_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354112LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_SEND_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_DATA_BYTES, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 209);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "SELECT LOC FROM DEPT WHERE DEPTNO=:1 AND DNAME=:2 AND LOC=:3 ");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_START_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354113LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_END_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354113LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_SERVER_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1000LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_DOWNLOAD_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_DATA_BYTES, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 195LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_CODE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1403LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_ERR_MSG, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "ORA-01403: 未找到任何数据\n");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_CODE_STRING, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "ORA-01403");

    p_st_field = FrReportOpt_get_field(p_st_report_data, "request.sql.text", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "select loc from dept where deptno=:1  and dname=:2  and loc=:3 ");

    p_st_field = FrReportOpt_get_field(p_st_report_data, "request.sql.batch.size", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, "request.sql.batch.1.param.size", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 3LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data, "request.sql.batch.1.param.1", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "10");

    p_st_field = FrReportOpt_get_field(p_st_report_data, "request.sql.batch.1.param.2", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "ACCOUNTING");

    p_st_field = FrReportOpt_get_field(p_st_report_data, "request.sql.batch.1.param.3", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "NEW YORK");

    p_st_field = FrReportOpt_get_field(p_st_report_data, "response.sql.batch.1.rows", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1LL);

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_ora_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_ora_analyze(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ora_analyze()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
